import Coin_fly_to_wallet from "../../../Coin_fly_to_wallet";
import timePlugIn from "../../../ipha_tool/TimePlugIn";
import UIService from "../../services/uiService";
import UserService from "../../services/userService";
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";
import PageBase from "./pageBase";


const { ccclass, property } = cc._decorator;

@ccclass
export default class TyOverPage extends PageBase {

    public pageName: string = "通用失败页";

    // @property({ type: cc.Label, displayName: "是否过关文本" })
    // private message: cc.Label = null;

    @property(cc.Node)
    passNode:cc.Node = null

    @property(cc.Node)
    passNode2:cc.Node = null

    @property(cc.Node)
    lossNode:cc.Node = null

    @property(cc.Node)
    caiNode:cc.Node = null

    @property(cc.Node)
    bgkuang:cc.Node = null


    setCloseDoFun(colseDoFun: Function) {
        this.colseDoFun = colseDoFun;
    }

    start() {

        this._init();
       
        // sers().platformSrv.platformApi.recordedGameScreenStop()
    }

    protected _init() {
        this.open();

    }

    private moshi = 1
    initData(data) {
        cc.log(data)

        this.moshi = data.newgame

        this.passNode.active = data.pass
        this.lossNode.active = !data.pass

        if(!data.pass){
            // this.node.getChildByName('bg').opacity=0
            this.bgkuang.opacity = 0
            this.lossNode.getChildByName('icon').active = false
            this.lossNode.getChildByName('btnNode').active = false
            this.lossNode.active = false
            this.caiNode.active = true
            this.lossNode.getChildByName('jinbishu').children[0].getComponent(cc.Label).string = 'x'+this.huodejinbi
            this.scheduleOnce(()=>{
                this.lossNode.active = true
                this.caiNode.active = false
                this.bgkuang.opacity = 255
                this.node.getChildByName('bg').opacity= 150
                this.lossNode.getChildByName('icon').active = true
                this.lossNode.getChildByName('btnNode').active = true
            },2)

        }
        if(data.pass){
            if(data.starsNumber){
                this.huodejinbi = +Math.floor(data.starsNumber/10) || 1
                this.starsNumber = data.starsNumber
                this.passNode.getChildByName('jinbishu').children[0].getComponent(cc.Label).string = 'x'+this.huodejinbi
            }
            // this.passNode.getChildByName('xx').children[0].getComponent(cc.Label).string = 'x'+this.starsNumber
            this.passNode.getChildByName('jinbishu').children[0].getComponent(cc.Label).string = 'x'+this.huodejinbi
            

            if(data.passPage2){
                this.passNode.active = false
                this.caiNode.active = true
                this.bgkuang.opacity = 0
                this.scheduleOnce(()=>{
                    this.bgkuang.opacity = 255
                    this.caiNode.active = false
                    this.passNode.active = false
                    this.passNode2.active = true
                    this.passNode2.getChildByName('jinbishu').children[0].getComponent(cc.Label).string = 'x0'
                    this.passNode.getChildByName('title').active = false
                    
    
                    let xinjilu = this.passNode2.getChildByName('xinjilu')
                    xinjilu.active = data.xinjilu
                    xinjilu.scale = 1.2
    
                    cc.tween(xinjilu).delay(0.5).to(0.2,{scale:0.8}).to(0.1,{scale:1}).start()
                    this.passNode2.getChildByName('xx').children[0].getComponent(cc.Label).string = 'x'+this.starsNumber
                    this.scheduleOnce(()=>{
                        this.node.getComponent(Coin_fly_to_wallet).playAnim(data.starsNumber>20? 20 : data.starsNumber)
            
                        this.scheduleOnce(()=>{
                            this.passNode2.getChildByName('xx').children[0].getComponent(cc.Label).string = 'x0'
                            this.passNode2.getChildByName('jinbishu').children[0].getComponent(cc.Label).string = 'x'+this.huodejinbi+''
                            // sers().userSrv.addCoin(this.huodejinbi)
                            // sers().uiSrv.showMessage(`获得${this.huodejinbi}金币`)
                        },1.5)
                        }
                    )
                },2)

               
            }
            sers().userSrv.addCoin(this.huodejinbi)
           

        }
        this.touzi()
    }
    starsNumber = 200
    huodejinbi = 45
    show = false
    fanbei(evt){

        let touzi = this.passNode.getChildByName('touzi')
        let touzi2 = this.passNode2.getChildByName('touzi')
        touzi.stopAllActions()
        touzi2.stopAllActions()


        let node = evt.currentTarget
        sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
            let touzi = this.passNode.getChildByName('touzi')
            touzi.stopAllActions()
            let touzi2 = this.passNode2.getChildByName('touzi')
            touzi2.stopAllActions()
            let beishu = 2
            let abs = Math.abs(touzi.x)
            if(abs<42.5){
                beishu = 5
            }else if(abs<92.5){
                beishu = 4
            }else if(abs<152.5){
                beishu = 3
            }
            // node.active = false
            node.getComponent(cc.Button).interactable = false
            this.show = true
            sers().userSrv.addCoin(this.huodejinbi*beishu-this.huodejinbi)
            sers().uiSrv.showMessage(`获得${this.huodejinbi*beishu}金币`)
        }, (err) => {
            
        }, ()=>{
            this.touzi()
        }, {
            scene: '游戏结束页面',
            event: '金币翻倍',
          },'common_video');
       
    }

    tiqianlingqu(evt){
        let node = evt.currentTarget

        sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
            let touzi = this.passNode.getChildByName('touzi')
            touzi.stopAllActions()
            let touzi2 = this.passNode2.getChildByName('touzi')
            touzi2.stopAllActions()
            let touzi3 = this.lossNode.getChildByName('touzi')
            touzi3.stopAllActions()

            let beishu = 2
            let abs = Math.abs(touzi.x)
            if(abs<42.5){
                beishu = 5
            }else if(abs<92.5){
                beishu = 4
            }else if(abs<152.5){
                beishu = 3
            }
            node.getComponent(cc.Button).interactable = false
            sers().userSrv.addCoin(this.huodejinbi*beishu)
            sers().uiSrv.showMessage(`获得${this.huodejinbi*beishu}金币`)
        }, (err) => {
            
        }, ()=>{
            this.touzi()
        }, {
            scene: '游戏结束页面',
            event: '提前领取',
          },'common_video');
    }

    touzi(){
        let touzi = this.passNode.getChildByName('touzi')
        let touzi2 = this.passNode2.getChildByName('touzi')
        let touzi3 = this.lossNode.getChildByName('touzi')


        touzi.stopAllActions()
        touzi2.stopAllActions()
        touzi3.stopAllActions()
        let time = 0.5
        cc.tween(touzi).to(time,{x:222.5}).to(time,{x:0}).to(time,{x:-222.5}).to(time,{x:0}).union().repeatForever().start()
        cc.tween(touzi2).to(time,{x:222.5}).to(time,{x:0}).to(time,{x:-222.5}).to(time,{x:0}).union().repeatForever().start()
        cc.tween(touzi3).to(time,{x:222.5}).to(time,{x:0}).to(time,{x:-222.5}).to(time,{x:0}).union().repeatForever().start()

    }


    share() {
        sers().platformSrv.platformApi.shareAppMessage('',`我智商高达${this.starsNumber}，已经爆表了，你也来试试吧。`)
    }

    newGame() {
        if(!this.show){
            this.show=true
            sers().uiSrv.showMessage(`获得${this.huodejinbi}金币`)
        }
        mgrs().soundMgr.playBtnSound();

        if(this.moshi!=2 && this.moshi!=3){
            if(sers().userSrv.power<=0){
                sers().uiSrv.showUserGetPowerPage();
                return
            }
            sers().userSrv.addPower(-1)
        }
        
        mgrs().gameMgr.enterMode(this.moshi);
    }

    restartGame() {
        mgrs().soundMgr.playBtnSound();

        if(this.moshi!=2 && this.moshi!=3){
            if(sers().userSrv.power<=0){
                sers().uiSrv.showUserGetPowerPage();
                return
            }
            sers().userSrv.addPower(-1)
        }
       

        mgrs().gameMgr.enterMode(this.moshi);
    }

    open() {
        UIService.openPageBasicAni(this.showNode, true)
            .then(() => {

            }).catch(() => {

            });
    }

    close() {
        sers().platformSrv.platformApi.hideBanner()

        this.closeAni()
            .then(() => {
                // this.colseDoFun && this.colseDoFun();

                sers().uiSrv.deletePageRecord(this.id);

                this.node.destroy();
      
            })
            .catch();
    }

    onlyClose() {
        this.closeAni()
            .then(() => {
                sers().uiSrv.deletePageRecord(this.id);
                this.node.destroy();
            })
            .catch();
    }

    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        mgrs().gameMgr.backHomePage();
        this.close();
        cc.log("OnBtnClose");
    }

    backIndex() {
        this.OnBtnClose()
    }

    protected closeAni(isResume: boolean = false) {
        return UIService.closePageBasicAni(this.showNode, isResume);
    }

    update(dt) {
        let touzi = this.passNode.getChildByName('touzi')
        let touzi2 = this.passNode.getChildByName('touzi')
        let touzi3 = this.lossNode.getChildByName('touzi')


        let beishu = 2
        let abs = Math.abs(touzi.x)
        if(abs<42.5){
            beishu = 5
        }else if(abs<92.5){
            beishu = 4
        }else if(abs<152.5){
            beishu = 3
        }
        this.passNode.getChildByName('btnLayout').getChildByName('fanbei').children[0].children[1].getComponent(cc.Label).string = `${beishu}倍领奖`

        let beishu2 = 2
        let abs2 = Math.abs(touzi2.x)
        if(abs2<42.5){
            beishu2 = 5
        }else if(abs2<92.5){
            beishu2 = 4
        }else if(abs2<152.5){
            beishu2 = 3
        }
        this.passNode2.getChildByName('btnLayout').getChildByName('fanbei').children[0].children[1].getComponent(cc.Label).string = `${beishu2}倍领奖`


        let beishu3 = 2
        let abs3 = Math.abs(touzi3.x)
        if(abs3<42.5){
            beishu3 = 5
        }else if(abs3<92.5){
            beishu3 = 4
        }else if(abs3<152.5){
            beishu3 = 3
        }
        this.lossNode.getChildByName('btnNode').getChildByName('fanbei').children[0].children[1].getComponent(cc.Label).string = `提前${beishu3}倍领奖`
    }
}
